<?php

/**
 * This is the model class for table "categoria".
 *
 * The followings are the available columns in table 'categoria':
 * @property integer $id
 * @property string $desc_categoria_es
 * @property string $desc_categoria_en
 * @property integer $activo
 * @property string $imagen
 * @property string $slug
 *
 * The followings are the available model relations:
 * @property Producto[] $productos
 */
class Categoria extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Categoria the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'categoria';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('desc_categoria_es, desc_categoria_en', 'required'),
			array('activo', 'numerical', 'integerOnly'=>true),
			array('desc_categoria_es, desc_categoria_en', 'length', 'max'=>70),
            array('slug', 'length', 'max'=>80),
            array('imagen', 'file', 'allowEmpty' => true, 'types' => 'jpg, jpeg, png, gif', 'maxSize'=>524288), //512kb
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, desc_categoria_es,desc_categoria_en, activo, imagen', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'productos' => array(self::HAS_MANY, 'Producto', 'categoria_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'desc_categoria_es' => 'Categoría',
            'desc_categoria_en' => 'Category',
			'activo' => 'Activo',
            'imagen' => 'Imagen'
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('desc_categoria_es',$this->desc_categoria_es,true);
        $criteria->compare('desc_categoria_en',$this->desc_categoria_en,true);
		$criteria->compare('activo',$this->activo);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    public function getCategoriasActivas(){
        return $this->model()->findAll("activo = 1");
    }

    public function getCategoriaBySlug($slug){
        return $this->model()->find("activo = 1 and slug = :slug", array(":slug" => $slug));
    }
}